<?php

header('Content-type: text/html; charset=utf-8'); 

if(!(count($_SESSION)>0)) {
	session_set_cookie_params(10800);
	session_start();
}
error_reporting(E_ALL);
ini_set('display_errors', '1');


require_once dirname(__FILE__).'/cf/config.php';
require_once cf\Config::path.'param.php';
require_once cf\Config::path.'user.php';
require_once cf\Config::path.'image.php';
require_once cf\Config::path.'api/shop.php';

define('SMARTY_DIR','./smarty/');
require_once SMARTY_DIR . 'Smarty.class.php';

$smarty = new Smarty;
$smarty->compile_check = true;
$smarty->debugging = false;
$smarty->addPluginsDir(cf\Config::path.'components/');

date_default_timezone_set('UTC');
setlocale(LC_ALL,"en_US.utf8");

cf\Image::$USE_IMAGEMAGICK = cf\Config::use_imagemagick;

$smarty->assign('show_error','');

if(isset($_SESSION['skidka']) && $_SESSION['skidka'] != '') {
	$smarty->assign('showskidka','false');
} else {
	$smarty->assign('showskidka','true');
	$_SESSION['skidka'] = 1;
}


if (array_key_exists('log',$_POST) && array_key_exists('pwd',$_POST)) {
	try {
		cf\User::login(cf\param::get('log'), cf\param::get('pwd'), cf\param::exist('remember'));
	} catch(Exception $e) {
	}
	if (!cf\User::getLoggedIn()) {
		$smarty->assign('show_error','Не удалось авторизироваться. Пожалуйста, проверьте правильность е-мейла и пароля.');
	}
}

$user = cf\User::getLoggedIn();
$smarty->assign('user',$user);

$smarty->assign('categories', cf\api\shop\getCategoriesTree(null, false, 0, '/catalog') );

function showCatsInLeftMenu($cat)
{
	global $smarty;
	
	$leftMenuRootCatId = $cat['children_count'] ? $cat['id'] : $cat['parent_id']; //empty($childrenCats) ? $cat['parent_id'] : $cat['id'];
	if ($leftMenuRootCatId) {
		$leftMenuRootCategory = cf\api\shop\getCategory($leftMenuRootCatId, '/catalog');
		$smarty->assign('leftMenuRootCategory', $leftMenuRootCategory);
		$smarty->assign('leftMenuCategories', cf\api\shop\getCategoriesTree($leftMenuRootCategory['id'], false, 1, $leftMenuRootCategory['path']) );
		$smarty->assign('leftMenuTpl', 'left_menu_categories');
	}
}

function show($template, $name, $crumbs=array(), $title='', $keywords='', $descr='')
{
	global $smarty, $lang;

	$smarty->assign('name',$name);
	$smarty->assign('crumbs',$crumbs);
	$smarty->assign('title',$title);
	$smarty->assign('keywords',$keywords);
	$smarty->assign('descr',$descr);
	
	$smarty->assign('tpl', "$template.tpl");

	$smarty->display('main.tpl');
}

$find = '';
$smarty->assignByRef('find',$find);

require_once 'cf/api/gallery.php';
$smarty->assign('topAlbums',cf\api\gallery\getAlbums());


$smarty->assign('banners1', cf\api\gallery\getImages('gallery1'));
$smarty->assign('banners2', cf\api\gallery\getImages('gallery2'));
$smarty->assign('banners3', cf\api\gallery\getImages('gallery3'));
$smarty->assign('banners4', cf\api\gallery\getImages('gallery4'));
$smarty->assign('banners5', cf\api\gallery\getImages('gallery5'));
$smarty->assign('banners6', cf\api\gallery\getImages('gallery6'));


$smarty->assign(
	'latestNews', 
	cf\query2arrays(
		'SELECT cf_news.id,dt,short_txt FROM cf_news INNER JOIN cf_news_in_category ON cf_news.id=news_id WHERE category_id=1 ORDER BY dt DESC LIMIT 2'
	)
);

$smarty->assign(
	'latestExpo', 
	cf\query2array(
		'SELECT cf_news.id,dt,short_txt FROM cf_news INNER JOIN cf_news_in_category ON cf_news.id=news_id WHERE category_id=2 ORDER BY dt DESC LIMIT 1'
	)
);


function getPromo($cat) 
{
	global $smarty;
	
	$promo = false;
	$qPromo = cf\createQuery("SELECT * FROM promobox WHERE group_id=:gid");
	foreach ($cat['branch'] as $gr) {
		$p = cf\query2array($qPromo,array('gid'=>$gr['id']));
		if (!empty($p)) {
			$promo = $p;
		}
	}
	$smarty->assign('promo',$promo);
}

?>
